package com.bbh.plugin.single.privilege.demo.framework;

import freemarker.ext.beans.BeansWrapper;
import freemarker.ext.beans.BeansWrapperBuilder;
import freemarker.template.Configuration;
import freemarker.template.TemplateHashModel;
import freemarker.template.TemplateModelException;
import org.springframework.boot.config.mvc.custom.WebCallback;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import java.util.Map;
import java.util.Properties;

/**
 * Created by roman_000 on 2016/6/26.
 */
@Component
public class WebCallbackImpl implements WebCallback {
    private String STATICS = "statics";

    @Override
    public void FreemarkerCallback(FreeMarkerConfig freeMarkerConfig, Properties settings, Map<String, Object> variables) {
        Object obj = variables.get(STATICS);
        Map map = (Map) obj;
        BeansWrapper wrapper = new BeansWrapperBuilder(Configuration.VERSION_2_3_23).build();
        TemplateHashModel staticModels = wrapper.getStaticModels();
        try {
            TemplateHashModel statics = (TemplateHashModel) staticModels.get("java.util.UUID");
            map.put("UUID", statics);
        } catch (TemplateModelException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

    }
}
